<?php

namespace App\Models;

use Session;
use Cookie;

class Securitycodein extends BaseModel
{
    protected $table = "security_code_in";

    const PREFIX = 'ag_';


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 获取防伪码列表
     * @return mixed
     */
    public function getSecurityList()
    {
        $where = [];
        $where['bid'] = session('adminId');
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
        }
        return $rows;
    }

    /**
     * 保存防伪码
     */
    public function saveSecurity($request)
    {
        $Security = trim($request['securitycode']);
        $SecurityCode = explode(',', $Security);
        for ($i = 0; $i < count($SecurityCode) - 1; $i++) {
            $data['bid'] = session('adminId');
            $data['code'] = $SecurityCode[$i];
            $data['goods_id'] = $request['goods_id'];
            $data['order_id'] = $request['order_id'];
            $data['order_num'] = 1;
            $data['created_at'] = date("Y-m-d H:i:s", time());
            $data['updated_at'] = date('Y-m-d H:i:s', time());
            $this->add($data);
        }
        return $res = ['status' => 200, 'msg' => "防伪码保存成功"];
    }

    /**
     * 创建防伪码
     */
    public function creatSecurity($request)
    {
        $id = $request['order_id'];
        $order = new Order();
        $order_num = $order->getOne(array('id' => $id));
//        if($order){
//
//        }
        $securityarr = "";
        $CH_NY = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 'N', 'O', "P", 'Q', "R", 'S', 'T', 'U', 'V', 'W', "X", "Y", "Z");
        for ($i = 0; $i < $order_num['goods_num']; $i++) {
            $prfx = '';
            $randnum = rand(2, 4);
            for ($j = 0; $j < $randnum; $j++) {
                $prfx .= $CH_NY[array_rand($CH_NY)];
            }
            $time = time();
            $rand = 11 - $randnum;
            $offset = strlen($time) - $rand;
            $time_str = substr($time, $offset);
            $time_arr = str_split($time_str);
            for ($n = 0; $n < $rand; $n++) {
                $_temp = $time_arr[rand(0, count($time_arr) - 1)];
                $time_str = str_replace($_temp, rand(0, 9), $time_str);
            }
            $securityarr .= $prfx . $time_str . ',';
        }
        if (empty($securityarr)) {
            $res = ['status' => 0, 'msg' => '防伪码生成失败'];
            return $res;
        }
        $res = ['status' => 200, 'msg' => "防伪码生成成功", 'data' => $securityarr];
        return $res;
    }

}